package cn.edu.dlmu.cogo.register;

import cn.edu.dlmu.cogo.framework.annotation.Autowired;
import cn.edu.dlmu.cogo.framework.annotation.Controller;
import cn.edu.dlmu.cogo.framework.annotation.RequestMapping;
import cn.edu.dlmu.cogo.framework.annotation.RequestParam;
import cn.edu.dlmu.cogo.framework.config.JavaConfig;
import cn.edu.dlmu.cogo.framework.context.Environment;
import cn.hutool.core.lang.Console;

import java.util.List;

/**
 * @author hongda.li@hand-china.com 2022/3/10 21:34
 */

@Controller
@RequestMapping("/register-center")
public class CogoRegisterController {

    @Autowired
    private CogoRegisterCenter center;

    @RequestMapping("/get-server")
    public RegisterConfig getServerInfo(){
        return JavaConfig.getConfig(RegisterConfig.class);
    }

    @RequestMapping("/register")
    public String registerService(ServiceInstance instance){
        Console.log("{} [CogoRegisterController]接受到服务注册请求，请求信息[{}]", Environment.getSystemTime(), instance);
        if (instance.getServiceName() != null){
            boolean register = center.register(instance);
            if (register){
                return RegisterConstant.REGISTER_SUCCESS;
            }
        }
        return RegisterConstant.REGISTER_FAILURE;
    }

    @RequestMapping("/renew")
    public String renewService(@RequestParam("serviceName") String serviceName){
        Console.log("{} [CogoRegisterController]接受到服务续约请求，请求信息[{}]",Environment.getSystemTime(),serviceName);
        boolean renewService = center.renewService(serviceName);
        if (renewService){
            return RegisterConstant.RENEW_SUCCESS;
        }else {
            return RegisterConstant.RENEW_FAILURE;
        }
    }

    @RequestMapping("/get-services")
    public List<ServiceInstance> getServiceList(){
        return center.getServiceList();
    }

    @RequestMapping("/get-service")
    public ServiceInstance getService(@RequestParam("serviceName") String serviceName){
        return center.getService(serviceName);
    }

    @RequestMapping("/delete")
    public void deleteService(@RequestParam("serviceName") String serviceName){
        Console.log("{} [CogoRegisterController]接受到服务下线请求，请求信息[{}]",Environment.getSystemTime(),serviceName);
        center.removeService(serviceName);
    }

    @RequestMapping("/disable")
    public void disableService(@RequestParam("serviceName") String serviceName){
        Console.log("{} [CogoRegisterController]接受到禁用服务请求，请求信息[{}]",Environment.getSystemTime(),serviceName);
        center.disableService(serviceName);
    }

    @RequestMapping("/enable")
    public String enableService(@RequestParam("serviceName") String serviceName){
        Console.log("{} [CogoRegisterController]接受到启用服务请求，请求信息[{}]",Environment.getSystemTime(),serviceName);
        boolean enableService = center.enableService(serviceName);
        if (enableService) {
            return RegisterConstant.ENABLE_SUCCESS;
        }else {
            return RegisterConstant.ENABLE_FAILURE;
        }
    }
}
